const rptUrl = 'http://tsf.erp-km.cn:1213/'
//imguploadurl:http://192.168.140.154:57252/api.ashx?c=WXImage&a=UploadToOss
// const rptUrl = 'http://192.168.140.154:7997/'
// const testUrl = 'http://192.168.88.102:1212/'
// const rptUrl = 'https://tsf.erp-km.cn/'

console.log(rptUrl)
const getPath = function (Controll, action) {
    return rptUrl + "api.ashx?c=" + Controll + "&a=" + action;
}
// 'MasterID': 1064
const YAPI = function (Controll, action, parma) {
    var PromiseAPI = new Promise((resolve, reject) => {
        var that = this;
        wx.request({
            url: getPath(Controll, action),
            data: parma,
            method: "POST",
            header: {
                "content-type": "application/json",
                "Caller": "kmerApiSDK"
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (err) {
                reject(err);
            },
        });
    })
    return PromiseAPI;
}

const UPLOAD = function (Controll, action, parma) {
    var PromiseAPI = new Promise((resolve, reject) => {
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                wx.uploadFile({
                    url: getPath(Controll, action), //仅为示例，非真实的接口地址getPath(Controll, action)
                    filePath: res.tempFilePaths[0],//
                    header: {
                        "content-type": "multipart/form-data"
                    },
                    name: "file",
                    formData: {
                        "basePath": "WXImg", "MasterID": parma.MasterID, "SerialNo": parma.SerialNo
                    },
                    success: function (res) {
                        resolve(res.data);
                    },
                    fail: function (err) {
                        reject(err);
                    },
                });
            },
            fail: function (err) {
                reject(err);
            }
        });
    })
    return PromiseAPI;
}

const checkNull = function (obj) {
    return typeof (obj) == 'undefined' || obj == null || obj == "";
}

const getUser = function () {
    var PromiseUser = new Promise((resolve, reject) => {
        var app = getApp();
        if (checkNull(app.globalData.UserInfo)) {
            reject(`没有UserInfo`);
        } else {
            resolve(app.globalData.UserInfo);
        }
    })
    return PromiseUser;
}

const hasUserAuth = function (doSuccess, doFail) {
    // 查看是否授权
    wx.getSetting({
        success: function (res) {
            if (res.authSetting["scope.userInfo"]) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                wx.getUserInfo({
                    withCredentials: true,
                    success: function (res) {
                        doSuccess(res);
                    },
                    fail: function (res) {
                        doFail(res);
                    }
                });
            } else {
                doFail(res);
            }
        }
    });
}

export { rptUrl, YAPI, UPLOAD, checkNull, getUser, hasUserAuth }